<?php
namespace app\macht\controller;
use app\common\controller\Platurl;
use app\common\model\CardConsume;
use app\common\model\CardUserGet;
use app\common\model\CardUserUpdate;
use app\common\model\LoginUsers;
use app\common\model\Merchant;
use app\common\model\Momerchant;
use app\common\model\OpenTemplate;
use app\common\model\Paylog;
use think\Controller;
use Wechat\Wxobjopen;

class Stati extends MachBase
{
    public function test(){
        $today = strtotime(date("Y-m-d"),time()) - 86400*3;
        $enday = $today+86399;
        echo date('Y-m-d H:i:s',$today);
        echo "<br />";
        echo date('Y-m-d H:i:s',$enday);
        $this->index();
   }

    //群发模版消息
    public function task_tpl_stati($data)
    {
//        file_put_contents('/data2/html/payos/test.txt', 'stati-1' . json_encode($data,JSON_UNESCAPED_UNICODE), FILE_APPEND);
        //初始化微信类
        if (isset($data['openid'])) {
            $Platurl = new Platurl();
            $options = $Platurl->getagenttplmsg($data['mid']);
//            $options = $this->option($wx_open_id);
            $Momach = new Momerchant();
            $weObj = new Wxobjopen($options);
//            $wx_open_id = $Momach->where(['id'=>$data['top_mid']])->value('wx_open_id');
//            //模板ID
//            $OpenTemplate = new OpenTemplate();
//            $templateid = $OpenTemplate->where(['type' => 'pre_man_tpl', 'wx_open_id' => $wx_open_id])->value('template_id');
            $templateid = $Platurl->getagenttplid($data['mid']);
            $template['touser'] = $data['openid'];
            $template['template_id'] = $templateid;
            $template['url'] = '';
            $template['topcolor'] = "#7B68EE";
            $template['data']['first'] = ['value' => '请查收昨日会员卡运营数据', 'color' => "#743A3A"];
            $template['data']['keyword1'] = ['value' => $data['keyword1'], 'color' => '#000'];
            $template['data']['keyword2'] = ['value' => date("Y-m-d H:i:s", time()), 'color' => '#FF0000'];
            $template['data']['remark'] = ['value' => $data['remark'], 'color' => '#000'];
            //发送
//            file_put_contents('/data2/html/payos/test.txt', 'stati-2' . json_encode($template,JSON_UNESCAPED_UNICODE), FILE_APPEND);
            $res = $weObj->sendTemplateMessage($template);
//            file_put_contents('/data2/html/payos/test.txt', 'stati-3' . json_encode($res,JSON_UNESCAPED_UNICODE), FILE_APPEND);
            return $res;
        }
    }


    private function index(){
        //top_mid
//
//        $Momerchant = new Momerchant();
//        $machlist=$Momerchant->field('id')->select();
        $machlist = [1];
        //用户列表
        $res = '';
        foreach($machlist as $ks=>$vs){
            $arr['top_mid']=1;
            $userlist = $this->getsendren($arr);

            //获取统计，组合数据
            foreach ($userlist as $k=>$v){
                $arrq['mid'] = $v['mid'];
                $balance = $this->conutbalance($arrq);        //余额统计
                $cards = $this->conutcards($arrq);            //卡券统计
                $cardus = $this->conutcardus($arrq);         //会员卡统计
                $cash = $this->cashPay($arrq);         //会员卡统计
                $pay = $this->conutpay($arrq);               //支付统计
                $sum = $this->sum_data($pay,$cash);
                //组合数据
                $data['openid'] = $v['openid'];
                $data['top_mid'] = $arr['top_mid'];
                $data['mid'] = $arrq['mid'];
                $data['keyword1'] = date("Y-m-d", time() - 86399) .'日统计报告';
                $data['remark'] =
                    "微信支付:".$pay['wx_pay_freq'].'笔 / '.$pay['wx_pay_sum']."元\n".
                    "支付宝支付:".$pay['ali_pay_freq']."笔 / ".$pay['ali_pay_sum']."元\n".
                    "现金支付:".$cash['pay_freq']."笔 / ".$cash['pay_sum']."元\n".
                    "收款总计:".$sum['pay_freq']."笔 / ".$sum['pay_price']."元\n".
                    "会员卡支付总计:".$balance['pay_freq']."笔 / ".$balance['pay_sum']."元\n".
                    "-------------------------\n".
                    "当日会员:"."新增".$cardus['get_freq']."人 / "."总计".$cardus['sum_freq']."人\n".
                    "当日卡券:"."领取".$cards['get_freq']."人 / "."核销".$cards['con_freq']."人\n";
                $res = $this->sendtpl($data);
            }
        }
        dump($res);
    }

    //总计
    private function sum_data($pay,$balance){
        $sum['pay_freq'] = $pay['wx_pay_freq'] + $pay['ali_pay_freq'] + $balance['pay_freq'];        //笔数
        $sum['pay_price'] = $pay['wx_pay_sum'] + $pay['ali_pay_sum'] + $balance['pay_sum'];        //金额
        return $sum;
    }

    //余额统计
    private function conutbalance($arr){
        $CardUserUpdate = new CardUserUpdate();
        //条件
        $today = strtotime(date("Y-m-d"),time()) - 86400;
        $enday = $today + 86399;

        //储值
//        $mab['create_time'] = ['between',[$today,$enday]];
//        $mab['mid'] = ['in',$arr['mid']];;
//        $mab['add_balance'] = ['neq','0'];
//        $mab['type'] = 1;
//        $arr['pay_freq_cz'] = $CardUserUpdate->where($mab)->count('id');                  //储值次数
//        $arr['pay_balance'] = $CardUserUpdate->where($mab)->sum('pay_balance');        //支付余额
//        $arr['add_balance'] = $CardUserUpdate->where($mab)->sum('add_balance');        //增加余额

        //消费
        $mad['create_time'] = ['between',[$today,$enday]];
        $mad['mid'] = ['in',$arr['mid']];
        $mad['del_balance'] = ['neq','0'];
        $mad['type'] = 2;
        $arr['pay_freq'] = $CardUserUpdate->where($mad)->count('id');                  //消费次数
        $arr['pay_sum'] = $CardUserUpdate->where($mad)->sum('del_balance');        //消费余额
        return $arr;
    }

    //现金统计
    public function cashPay($arr){
        $Paylog = new Paylog();
        //条件
        $today = strtotime(date("Y-m-d"),time()) - 86400;        //开始
        $enday = $today + 86399;        //结束
        //现金查询
        $mab['add_time'] = ['between',[$today,$enday]];        //时间
        $mab['mid'] = ['in',$arr['mid']];
        $mab['pay_fs'] = 4;
        $mab['pay_statu'] = 1;
        $arr['pay_freq'] = $Paylog->where($mab)->count();    //现金支付次数
        $arr['pay_sum'] = $Paylog->where($mab)->sum('price');     //现金支付金额
        return $arr;
    }

    //卡券统计
    private function conutcards($arr){
        $CardUserGet = new CardUserGet();
        $CardConsume = new CardConsume();
        //条件
        $today = strtotime(date("Y-m-d"),time()) - 86400;
        $enday = $today + 86399;
        $map['create_time'] = ['between',[$today,$enday]];
        $map['card_type'] = ['neq','MEMBER_CARD'];
        $map['mid'] = ['in',$arr['mid']];
        //统计
        //核销
        $mab['create_time'] = ['between',[$today,$enday]];
//        $mab['card_type'] = ['neq','MEMBER_CARD'];
        $mab['mid'] = ['in',$arr['mid']];
        $arr['get_freq'] = $CardUserGet->where($map)->count('id');      //领取数量
        $arr['con_freq'] = $CardConsume->where($mab)->count('id');      //核销数量
        return $arr;
    }

    //会员卡统计
    public function conutcardus($arr){
        $CardUserGet = new CardUserGet();
        //条件
        $today = strtotime(date("Y-m-d"),time()) - 86400;
        $enday = $today + 86399;
        $map['create_time'] = ['between',[$today,$enday]];
        $map['card_type'] = ['eq','MEMBER_CARD'];
        $map['mid'] = ['in',$arr['mid']];
        $map['have'] = 1;
        $arr['get_freq'] = $CardUserGet->where($map)->count('id');      //领取人数
        $arr['fire_freq'] = $CardUserGet->where($map)->count('id');      //激活人数
        //总数
        $mad['card_type'] = ['eq','MEMBER_CARD'];
        $mad['mid'] = ['in',$arr['mid']];
        $mad['have'] = 1;
        $arr['sum_freq'] = $CardUserGet->where($mad)->count('id');      //总数
        //删除
        $mab['create_time'] = ['between',[$today,$enday]];
        $mab['card_type'] = ['eq','MEMBER_CARD'];
        $mab['have'] = 2;
        $mab['mid'] = ['in',$arr['mid']];
        $arr['del_freq'] = $CardUserGet->where($mab)->count('id');      //删除人数
        return $arr;
    }

    //支付统计
    private function conutpay($arr){
        $Paylog = new Paylog();
        //条件
        $today = strtotime(date("Y-m-d"),time()) - 86400;
        $enday = $today + 86399;
        //微信查询
        $map['add_time'] = ['between',[$today,$enday]];        //时间
        $map['mid'] = ['in',$arr['mid']];
        $map['pay_fs'] = ['in',[0,2]];
        $map['pay_statu'] = 1;
        $arr['wx_pay_freq'] = $Paylog->where($map)->count('mid');    //微信支付次数
        $arr['wx_pay_sum'] = $Paylog->where($map)->sum('price');     //微信支付金额
        //支付宝查询
        $mab['add_time'] = ['between',[$today,$enday]];        //时间
        $mab['mid'] = ['in',$arr['mid']];
        $mab['pay_fs'] = ['in',[1,3]];
        $mab['pay_statu'] = 1;
        $arr['ali_pay_freq'] = $Paylog->where($mab)->count('mid');    //微信支付次数
        $arr['ali_pay_sum'] = $Paylog->where($mab)->sum('price');     //微信支付金额
        return $arr;
    }

    //发送
    private function sendtpl($data){
        if($data){
//            $res = action('merchant/jobmach/task_tpl_stati',array($data));
            $res = $this->task_tpl_stati($data);
            return $res;
        }
    }

    //获取发送人
    private function getsendren($arr){
        $LoginUsers = new LoginUsers();
        $arrs['top_mid'] = $arr['top_mid'];
        //店长
        $mer_mid = $this->getstamid($arr);
        if($mer_mid!==''){
            $smerid = explode(',', $mer_mid);
            $len = count($smerid);
            if ($len > 1){
                foreach($smerid as $k=>$v){
                    $msid[]=array(array('like','%,'.$v),array('like',$v.',%'),array('like','%,'.$v.',%'),array('eq',$v),'or');
                }
                $map['mid']=array($msid,'or');
            }else{
                $map['mid']=$mer_mid;
            }
        }else{
            $map['mid']=0;
        }
        $map['level'] = ['eq',3002];
        $map['openid']=array('neq','');
        $map['is_report'] = 1;
        $userlist = $LoginUsers->where($map)->field('id,top_mid,mid,openid')->select();
//        $reslist = array_merge($master,$userlist);
        $reslist = $userlist;
        return $reslist;
    }

    //获取mid
    private function getstamid($arr){
        //mid
        $Merchant = new Merchant();
        $midlist = $Merchant->where(['top_mid' => $arr['top_mid']])->field('id')->select();
        $smid=[];
        foreach ($midlist as $k => $v) {
            $smid[] = $v['id'];
        }
        $mid = implode(',', $smid);
        return $mid;
    }



}
